package com.dgut.controller;

import com.dgut.constant.JwtClaimsConstant;
import com.dgut.constant.MessageConstant;
import com.dgut.dto.RegisterDTO;
import com.dgut.dto.UserDTO;
import com.dgut.entity.Employee;
import com.dgut.entity.User;
import com.dgut.properties.JwtProperties;
import com.dgut.result.Result;
import com.dgut.service.UserService;
import com.dgut.utils.AliOssUtil;
import com.dgut.utils.JwtUtil;
import com.dgut.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@Slf4j
@Api(tags = "通用接口")
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody UserDTO userDTO) {

        log.info("用户登录，用户信息：{}", userDTO);
        //登录验证
        Employee employee = userService.login(userDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, employee.getEmpId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        LoginVO loginVO = LoginVO.builder()
                .id(employee.getEmpId())
                .account(employee.getAccount())
                .token(token)
                .type(employee.getType())
                .build();

        log.info("用户登录成功，生成jwt令牌：{}",token);
        return Result.success(loginVO);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        log.info("用户注册，用户信息：{}", registerDTO);
        userService.register(registerDTO);
        log.info("用户注册成功");
        return Result.success();
    }

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传：{}", file);

        try {
            //获取原始文件名
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.randomUUID() + extension;
            //获取请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), fileName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

    @ApiOperation("获取注册数据")
    @GetMapping("/registerData")
    public Result getRegisterData() {
        List<User> list = userService.getRegisterData();
        return Result.success(list);
    }
}
